/**
 * Check if an element has a class
 * @param {HTMLElement} ele
 * @param {string} cls
 * @returns {boolean}
 */
export function hasClass(ele: HTMLElement, cls: string) {
  return !!ele.className.match(new RegExp("(\\s|^)" + cls + "(\\s|$)"));
}

/**
 * Add class to element
 * @param {HTMLElement} ele
 * @param {string} cls
 */
export function addClass(ele: HTMLElement, cls: string) {
  if (!hasClass(ele, cls)) ele.className += " " + cls;
}

/**
 * Remove class from element
 * @param {HTMLElement} ele
 * @param {string} cls
 */
export function removeClass(ele: HTMLElement, cls: string) {
  if (hasClass(ele, cls)) {
    const reg = new RegExp("(\\s|^)" + cls + "(\\s|$)");
    ele.className = ele.className.replace(reg, " ");
  }
}

/**
 * 判断是否是外部链接
 *
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path: string) {
  const isExternal = /^(https?:|http?:|mailto:|tel:)/.test(path);
  return isExternal;
}

/**
 * 设置Style属性
 *
 * @param propName
 * @param value
 */
export function setStyleProperty(propName: string, value: string) {
  document.documentElement.style.setProperty(propName, value);
}

/**
 * 下载blob数据
 * @param request Promise<Record>
 * @returns {void}
 */
export function downloadFile(request, fileName = "导出.xlsx") {
  request.then((response) => {
    const blob = new Blob([response.data]);
    if (window.navigator.msSaveOrOpenBlob) {
      navigator.msSaveBlob(blob, fileName);
    } else {
      const link = document.createElement("a");
      const evt = document.createEvent("HTMLEvents");
      evt.initEvent("click", false, false);
      link.href = URL.createObjectURL(blob);
      link.download = fileName;
      link.style.display = "none";
      document.body.appendChild(link);
      link.click();
      window.URL.revokeObjectURL(link.href);
    }
  });
}

/**
 * 下载压缩包
 * @param request Promise<Record>
 * @returns {void}
 */
export function downloadFileZip(request) {
  let fileName = "文件.zip";
  request.then((response) => {
    console.log("🚀 ~ request.then ~ response:", response);
    const headerString = response.headers["content-disposition"] + "";
    console.log("🚀 ~ request.then ~ headerString:", headerString);
    const filenameRegex =
      /(?:^|\s)filename(?:\*=UTF-8'\''|")?(.*?)(?:UTF-8'\''|")?(?=\s|$)/i;
    const filenameMatch = headerString.match(filenameRegex);

    if (filenameMatch) {
      fileName = filenameMatch[1].substring(1);
      fileName = fileName.substring(0, fileName.length - 1);
      console.log(fileName);
    }

    const blob = new Blob([response.data], {
      type: "application/octet-stream",
    });
    if (window.navigator.msSaveOrOpenBlob) {
      navigator.msSaveBlob(blob, fileName);
    } else {
      const link = document.createElement("a");
      link.href = URL.createObjectURL(blob);
      link.download = fileName;
      link.style.display = "none";
      document.body.appendChild(link);
      link.click();
      window.URL.revokeObjectURL(link.href);
    }
  });
}

/**
 * 下载blob数据
 * @param request Promise<Record>
 * @returns {void}
 */
export function convertToRate(val) {
  const num = (val * 100).toFixed(2);
  return num;
}
